package cn.ilovese.collect.serviceimpl.recommend.strategy;

import cn.ilovese.collect.datamapper.device.DeviceRequirementMapper;
import cn.ilovese.collect.datamapper.user.UserMapper;
import cn.ilovese.collect.po.task.Task;
import cn.ilovese.collect.service.recommend.strategy.RecommendStrategy;
import cn.ilovese.collect.util.RecommendUtil;
import cn.ilovese.collect.vo.task.TaskRecommendVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

public class TaskAcceptedStrategy implements RecommendStrategy {
    private static TaskAcceptedStrategy instance;

    private List<Task> taskAccepted;

    @Override
    public List<TaskRecommendVO> recommendExecute(List<Task> allTask,UserMapper userMapper, DeviceRequirementMapper deviceRequirementMapper) {
        List<TaskRecommendVO> res=new ArrayList<>();
        for(int i=0;i<allTask.size();i++){
            res.add(new TaskRecommendVO(allTask.get(i),userMapper.selectByPrimaryKey(allTask.get(i).getUserId()).getUsername()));
        }
        List<Double> acceptedTaskVector=new ArrayList<>();
        if(taskAccepted.size()==0){
            for(int i=0;i<res.size();i++) {
                res.get(i).setMatchity(0.0);
            }
            return res;
        }
        for(int i=0;i<taskAccepted.size();i++){
            List<Double> insertVector=RecommendUtil.initTaskVector(taskAccepted.get(i), deviceRequirementMapper.selectByTask(taskAccepted.get(i).getId()));
            if(i==0){
                for(int k=0;k<insertVector.size();k++){
                    acceptedTaskVector.add(insertVector.get(k));
                }
            }else{
                acceptedTaskVector=RecommendUtil.addVector(insertVector,acceptedTaskVector);
            }
        }
        for(int i=0;i<res.size();i++){
            res.get(i).setMatchity(
                    RecommendUtil.calculateMatchity(acceptedTaskVector,
                            RecommendUtil.initTaskVector(allTask.get(i), deviceRequirementMapper.selectByTask(allTask.get(i).getId())))
            );
        }
        return res;
    }

    public void setTaskAccepted(List<Task> taskAccepted) {
        this.taskAccepted = taskAccepted;
    }

    private TaskAcceptedStrategy(){
    }

    public static TaskAcceptedStrategy getInstance(){
        if(instance==null){
            instance=new TaskAcceptedStrategy();
        }
        return instance;
    }


}
